@tool
class_name Skill_Control_Sequence
extends SkillControl

## Sequence 串行
## sub_actions依次执行
#region 属性
## 是否允许子Action失败或中断，如果不允许失败或中断，则当子Action失败或中断的时候此节点会返回Failure
@export var is_allow_sub_action_failure_or_interrupt:bool = false
var cur_sub_action_idx:int = -1
var cur_sub_action:SkillAction:
	get:
		return get_sub_action(self.cur_sub_action_idx)
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
## 初始化
func init(action_idx:int = -1,init_data:Dictionary = {},handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
	super.init(action_idx,init_data,self_handle_func)
## 释放
func release(args:Dictionary = {},handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		step()
	super.release(args,self_handle_func)
## 打断
func interrupt(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		if cur_sub_action:
			cur_sub_action.interrupt()
	super.interrupt(self_handle_func)
## 完成
func complete(release_result:GameEnum.SkillReleaseResult = GameEnum.SkillReleaseResult.Success,handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.complete()
	super.complete(release_result,self_handle_func)
## 冷却
func cool(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.cool()
	super.cool(self_handle_func)
## 冷却完成
func cool_complete(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.cool_complete()
		cur_sub_action_idx = -1
	super.cool_complete(self_handle_func)
## 重置冷却
func reset_cool(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.reset_cool()
	super.reset_cool(self_handle_func)
## 销毁
func destroy(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.destroy()
	super.destroy(self_handle_func)
## 判断该技能能否释放
func check_can_release() -> GameEnum.SkillReleaseStatus:
	var res = super.check_can_release()
	if res != GameEnum.SkillReleaseStatus.Success:
		return res
	var next_idx = cur_sub_action_idx + 1
	var next_sub_action = get_sub_action(next_idx)
	if not next_sub_action:
		return GameEnum.SkillReleaseStatus.Failure
	res = next_sub_action.check_can_release()
	if res != GameEnum.SkillReleaseStatus.Success:
		return res
	return GameEnum.SkillReleaseStatus.Success
## 状态转换后的处理
func after_state_change(from_state:GameEnum.SkillActionState, to_state:GameEnum.SkillActionState):
	super.after_state_change(from_state,to_state)
func on_sub_action_release_completed(skill_action:SkillAction,release_result:GameEnum.SkillReleaseResult):
	if not is_releasing:
		return
	if not is_allow_sub_action_failure_or_interrupt and release_result != GameEnum.SkillReleaseResult.Success:
		result = release_result
		is_completed = true
	elif cur_sub_action_idx >= sub_actions.size() - 1 and (is_allow_sub_action_failure_or_interrupt or (not is_allow_sub_action_failure_or_interrupt and release_result == GameEnum.SkillReleaseResult.Success)):
		result = GameEnum.SkillReleaseResult.Success
		is_completed = true
	if check_is_completed():
		complete(result)
	else:
		step()
func on_sub_action_interrupted(skill_action:SkillAction):
	if not is_releasing:
		return
	if not is_allow_sub_action_failure_or_interrupt:
		result = GameEnum.SkillReleaseResult.Interrupted
		is_completed = true
	elif cur_sub_action_idx >= sub_actions.size() - 1 and (is_allow_sub_action_failure_or_interrupt):
		result = GameEnum.SkillReleaseResult.Success
		is_completed = true
	if check_is_completed():
		complete(result)
	else:
		step()
## 推进
func step():
	if skill_action_state == GameEnum.SkillActionState.Releasing:
		cur_sub_action_idx += 1
		if cur_sub_action:
			cur_sub_action.release(release_args)
		else:
			result = GameEnum.SkillReleaseResult.Success
			complete(result)
func get_sub_action(idx:int) -> SkillAction:
	return Utils.get_or_default(sub_actions,idx,null)
#endregion
#region 公共方法
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _get_configuration_warnings() -> PackedStringArray:
	var warnings = super._get_configuration_warnings()
	return warnings
#endregion
